<?php
//Functie maakt verbinding met database
function connect(){
  $db_host = 'localhost';
  $db_user = 'markpost_admin';
  $db_pass = 'WPkAQUr6';
  $db_name = 'markpost_main';
  
  mysql_connect($db_host, $db_user, $db_pass) or die ("Verbinding met server mislukt.");
  mysql_select_db($db_name) or die ("Verbinding met database mislukt.");
  
  unset($db_host);
  unset($db_user);
  unset($db_pass);
  unset($db_name);
}//connect

//Functie controleerd of input niet leeg is
function checkVariable($in, $iflink){
	if(empty($in))
		return false;
	if($iflink){
		return is_valid_url($in);
	}//if
	return true;
}//checkVariable

//Functie controleerd of url bestaat
function is_valid_url($url){
    if (!($url = @parse_url($url))){
        return false;
    }//if
 
    $url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];
    $url['path'] = (!empty($url['path'])) ? $url['path'] : '/';
    $url['path'] .= (isset($url['query'])) ? "?$url[query]" : '';
 
    if (isset($url['host']) AND $url['host'] != @gethostbyname($url['host'])){
        if (PHP_VERSION >= 5){
            $headers = @implode('', @get_headers("$url[scheme]://$url[host]:$url[port]$url[path]"));
        }else{
            if (!($fp = @fsockopen($url['host'], $url['port'], $errno, $errstr, 10))){
                return false;
            }//if
            fputs($fp, "HEAD $url[path] HTTP/1.1\r\nHost: $url[host]\r\n\r\n");
            $headers = fread($fp, 4096);
            fclose($fp);
        }//if
        return (bool)preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers);
    }//if
    return false;
}//is_valid_url

//Functie haalt alle redirects op uit de database
function getRedirects(){
	$allredirects = array();
	$sql = "SELECT * FROM redirect";
	$resultaat = mysql_query($sql);
	if($resultaat){
		$aantal = mysql_num_rows($resultaat);
		if($aantal > 0){
			while($rij = mysql_fetch_array($resultaat)){
				$name = $rij['Name'];
				$longlink = $rij['LongLink'];
				$shortlink = $rij['ShortLink'];
				$status = $rij['Status'];
				$temp = array(
							"ID" => $id,
							"Name" => $name,
							"LongLink" => $longlink,
							"ShortLink" => $shortlink,
							"Status" => $status
						);
				$allredirects[] = $temp;
			}//while
		}else{
			//genereer foutmelding
		}//if
	}else{
		//genereer foutmelding
	}//if
	return $allredirects;
}//getRedirects

//Functie maakt nieuwe redirect aan
function newRedirect($name, $longlink, $shortlink, $status){
	if(!checkVariable($name, false) && !checkVariable($longlink, true) && !checkVariable($shortlink, false) && !checkVariable($status)){
		connect();
		$sql = "INSERT INTO redirect (Name, LongLink, ShortLink, Status) VALUES ('".$name."', '".$longlink."', '".$shortlink."', '".$status."')";
		if (!mysql_query($sql)){
			//genereer foutmelding
		}//if
		mysql_close();
		//genereer melding gelukt
	}else{
		//genereer foutmelding
	}//if	
}//newRedirect

//Functie wijzigt redirect
function editRedirect($id, $name, $shortlink, $status){
	if(!checkVariable($id, false) && !checkVariable($name, false) && !checkVariable($shortlink, false) && !checkVariable($status)){
		connect();
		$sql = "UPDATE redirect SET Name='".$name."', LongLink='".$longlink."', ShortLink='".$shortlink."', Status='".$status."' WHERE ID=".$id;
		if (!mysql_query($sql)){
			//genereer foutmelding
		}//if
		mysql_close();
		//genereer melding gelukt
	}else{
		//genereer foutmelding
	}//if	
}//editRedirect

//Functie verwijderd redirect
function deleteRedirect($id){
	if(!checkVariable($id)){
		connect();
		$sql = "DELETE * FROM redirect WHERE ID=".$id;
		if (!mysql_query($sql)){
			//genereer foutmelding
		}//if
		mysql_close();
	}else{
		//genereer foutmelding
	}//if
}//deleteRedirect
?>